<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>GSAP 布局切换</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: Arial, sans-serif;
      background: linear-gradient(135deg, hsl(200, 60%, 85%), hsl(340, 70%, 85%));
      height: 100vh;
      overflow: hidden;
      display: flex;
      flex-direction: column;
    }

    /* Header */
    .header {
      height: 60px;
      background: rgba(255, 255, 255, 0.9);
      backdrop-filter: blur(10px);
      border-bottom: 1px solid rgba(0, 0, 0, 0.1);
      display: flex;
      align-items: center;
      padding: 0 20px;
      box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
      position: relative;
      z-index: 20;
    }

    .header .logo {
      font-size: 20px;
      font-weight: bold;
      color: #333;
    }

    .header .switch-button {
      background: #007bff;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 5px;
      cursor: pointer;
      margin-left: auto;
      z-index: 30;
    }

    /* 主容器 */
    .main-container {
      flex: 1;
      display: flex;
      overflow: hidden;
    }

    /* 经典布局：Sidebar */
    .sidebar {
      width: 250px;
      background: rgba(255, 255, 255, 0.9);
      backdrop-filter: blur(10px);
      border-right: 1px solid rgba(0, 0, 0, 0.1);
      padding: 20px;
      overflow-y: auto;
    }

    .sidebar ul {
      list-style: none;
    }

    .sidebar ul li {
      margin-bottom: 15px;
      padding: 10px;
      border-radius: 5px;
      cursor: pointer;
      transition: background 0.3s ease;
    }

    .sidebar ul li:hover {
      background: rgba(0, 0, 0, 0.1);
    }

    /* 内容区域 */
    .content {
      flex: 1;
      padding: 20px;
      overflow-y: auto;
      background: rgba(255, 255, 255, 0.8);
      backdrop-filter: blur(10px);
      min-width: 0; /* 允许宽度压缩 */
    }

    /* 单页布局：Header 菜单 */
    .spa-header .menu {
      display: flex;
      gap: 20px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      opacity: 0;
    }

    .spa-header .menu a {
      text-decoration: none;
      color: #333;
      font-weight: bold;
      padding: 10px;
      border-radius: 5px;
    }
  </style>
</head>
<body>
  <!-- Header -->
  <div class="header" id="header">
    <div class="logo">LOGO</div>
    <button class="switch-button" id="switchButton">切换到单页布局</button>
  </div>

  <!-- Main Container -->
  <div class="main-container is-classic" id="mainContainer">
    <div class="sidebar" id="sidebar">
      <ul>
        <li><a href="#">仪表盘</a></li>
        <li><a href="#">用户管理</a></li>
        <li><a href="#">设置</a></li>
        <li><a href="#">退出登录</a></li>
      </ul>
    </div>
    <div class="content">
      <h1>内容区域</h1>
      <p>这是内容区域。</p>
    </div>
  </div>

  <!-- 引入 GSAP -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
  <script>
    const switchButton = document.getElementById('switchButton');
    const mainContainer = document.getElementById('mainContainer');
    const sidebar = document.getElementById('sidebar');
    const header = document.getElementById('header');
    const content = document.querySelector('.content');
    let isClassic = true;

    // 初始化时间线
    const tl = gsap.timeline({ paused: true });

    // 配置动画
    tl.to(sidebar, {
      x: -250, // 向左移动 250px
      duration: 0.5,
      ease: "power2.inOut"
    })
    .to(content, {
      x: -250, // 同步向左移动
      width: "100%", // 直接设置宽度为 100%
      duration: 0.5,
      ease: "power2.inOut"
    }, "<") // 与上一个动画同步开始
    .add(() => {
      // 动画完成后移动菜单到 Header
      const menu = sidebar.querySelector('ul');
      menu.style.opacity = 0;
      header.appendChild(menu);
      header.classList.add('spa-header');
      gsap.to(menu, { opacity: 1, duration: 0.3 });
    });

    // 切换布局
    switchButton.addEventListener('click', () => {
      if (isClassic) {
        tl.play();
        switchButton.textContent = '切换到经典布局';
      } else {
        // 反向动画：先将内容区域向右移动，再展开 Sidebar
        tl.reverse();
        switchButton.textContent = '切换到单页布局';
        
        // 反向动画完成后移回菜单
        tl.eventCallback("onReverseComplete", () => {
          const menu = header.querySelector('ul');
          menu.style.opacity = 0;
          sidebar.appendChild(menu);
          header.classList.remove('spa-header');
        });
      }
      isClassic = !isClassic;
    });
  </script>
</body>
</html>
